package com.bootdo.common.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.ExcelFormatUtil;
import com.bootdo.system.domain.FileAuditDO;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.aspectj.weaver.tools.cache.AsynchronousFileCacheBacking.RemoveCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.domain.LogDO;
import com.bootdo.common.domain.PageDO;
import com.bootdo.common.service.LogService;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

@RequestMapping("/common/log")
@Controller
public class LogController {
	@Autowired
	LogService logService;
	String prefix = "common/log";

	@GetMapping()
	String log() {
		return prefix + "/log";
	}

	@ResponseBody
	@GetMapping("/list")
	PageDO<LogDO> list(@RequestParam Map<String, Object> params) {
		Query query = new Query(params);
		PageDO<LogDO> page = logService.queryList(query);
		return page;
	}

	/**
	 * 文件发送记录导出
	 */
	@ResponseBody
	@GetMapping("/listExcel")
	public ResponseEntity<byte[]> listExcel(String startDate, String endDate){
		try {
			Map<String, Object> params= new HashMap<>();
			params.put("startDate",startDate);
			params.put("endDate",endDate);
			List<LogDO> LogList = logService.queryList(params);
			return ExcelFormatUtil.buildResponseEntity(export(LogList), "管理员操作日志.xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private InputStream export(List<LogDO> LogList) {
		ByteArrayOutputStream output = null;
		InputStream inputStream1 = null;
		SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
		SXSSFSheet sheet = wb.createSheet();
		// 设置报表头样式
		CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
		CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

		// 每一列字段名
		String[] strs = new String[] { "用户名", "操作内容", "IP地址", "创建时间"};

		// 字段名所在表格的宽度
		int[] ints = new int[] { 5000, 5000, 5000, 10000};

		// 设置表头样式
		ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);

		if (LogList != null && LogList.size() > 0) {
			for (int i = 0; i < LogList.size(); i++) {
				LogDO logDO = LogList.get(i);
				SXSSFRow row = sheet.createRow(i + 1);
				int j = 0;
				SXSSFCell cell = row.createCell(j++);
				cell.setCellValue(logDO.getUsername());
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(logDO.getOperation());
				cell.setCellStyle(content);

				cell = row.createCell(j++);
				cell.setCellValue(logDO.getIp());
				cell.setCellStyle(content);

				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				cell = row.createCell(j++);
				cell.setCellValue(sdf.format(logDO.getGmtCreate())); // 发送时间
				cell.setCellStyle(content);
			}
		}
		try {
			output = new ByteArrayOutputStream();
			wb.write(output);
			inputStream1 = new ByteArrayInputStream(output.toByteArray());
			output.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (output != null) {
					output.close();
					if (inputStream1 != null){
						inputStream1.close();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return inputStream1;
	}


	@ResponseBody
	@PostMapping("/remove")
	R remove(Long id) {
		if (logService.remove(id)>0) {
			return R.ok();
		}
		return R.error();
	}

	@ResponseBody
	@PostMapping("/batchRemove")
	R batchRemove(@RequestParam("ids[]") Long[] ids) {
		int r = logService.batchRemove(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}
}
